import { _decorator, Component, find, Label, Node, ProgressBar, resources, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { StartSceneGameMgr } from './StartSceneGameMgr';
import { UILayerMgr } from './UILayerMgr';
import { Start } from './Start';
import { UserInfo } from './UserInfo';
import { AudioMgr } from '../utils/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('RoleChipCellItem')
export class RoleChipCellItem extends Component {

    @property(Sprite)
    sp: Sprite;
    @property(Sprite)
    qualityCellBg: Sprite;
    @property(Label)
    chipLabel:Label
    @property(ProgressBar)
    chipProgressBar:ProgressBar
    @property(Node)
    isSelectIcon:Node;

    roleCongfi;

    init(data) {
        this.roleCongfi=data;
        this.isSelectIcon.setPosition(0,-48,0);
        resources.load(`${data.imgPath}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.sp.spriteFrame=res;
        })
        resources.load(`qualityCellBg/${data.quality}/spriteFrame`,SpriteFrame,(err,res)=>{
            this.qualityCellBg.spriteFrame=res;
        })
        this.chipLabel.string=`${UserInfo.hasRoleChipArr[data.roleType]}/${data.totalChipNum}`;
        this.chipProgressBar.progress=UserInfo.hasRoleChipArr[data.roleType]/data.totalChipNum;

        if(StartSceneGameMgr.ins.roleSelectId==-data.roleType){
            this.isSelectIcon.active=true;

            tween(this.isSelectIcon)
            .by(0.3,{position:v3(0,5,0)})
            .by(0.3,{position:v3(0,-5,0)})
            .union()
            .repeatForever()
            .start()
        }
        else{
            this.isSelectIcon.active=false;
        }

    }

    onclickThisCom() {
        AudioMgr.ins.playOneShot("audio/clickBtn",0.8);

        console.log("点击角色item")
        if (StartSceneGameMgr.ins.roleSelectId == -this.roleCongfi.roleType) {
            return;
        }
        StartSceneGameMgr.ins.roleSelectId = -this.roleCongfi.roleType;
        StartSceneGameMgr.ins.saveGameDate();
        UILayerMgr.getIns.resfreshUI("equipPanel");
        Start.ins.initHomePanel();

    }
}


